"""
Description: 工具类
Author: 嘎嘣脆的贝爷
Date: 2024-11-05 17:33:23
LastEditTime: 2024-11-05 17:33:34
LastEditors: 嘎嘣脆的贝爷
"""

import asyncio, os


class Tool:
    # 异步包装器
    async def async_wrapper(self, sync_func, *kwargs):
        loop = asyncio.get_running_loop()
        # 使用线程池运行同步函数
        return await loop.run_in_executor(None, sync_func, *kwargs)

    async def save(self, fn: str, content: str):
        with await self.async_wrapper(open, fn, "w") as f:
            f.write(content)

    async def read(self, fn: str) -> str | None:
        if not os.path.isfile(fn):
            return None
        with await self.async_wrapper(open, fn, "r") as f:
            return f.read()


tool = Tool()
